<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="form" class="demo-form-inline">
        <el-form-item label="商品名称">
          <el-input v-model="form.name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <!-- <el-form-item label="商品分类">
            <el-select
              v-model="form.type"
              placeholder="选择商品类型"
              @change="selectChange"
            >
              <el-option
                v-for="(item, index) in Data"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item> -->
        <el-form-item label="商品分类">
          <el-cascader
            :options="options"
            :props="{
              emitPath: false,
              value: 'id',
              label: 'name',
              children: 'child',
            }"
            v-model="form.cat_id"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.time"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
            @change="timeChange"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="display: flex; justify-content: flex-end">
      <div class="salesAmount">销售金额：{{ money }}元</div>
    </div>
    <div>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column
          label="序号"
          :index="Method"
          type="index"
          prop="id"
          width="70"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="name" align="center" label="商品名称">
        </el-table-column>
        <el-table-column prop="goods_cat_name" align="center" label="商品分类">
        </el-table-column>

        <el-table-column prop="unit" align="center" label="商品单位">
        </el-table-column>
        <!-- <el-table-column align="center" label="水票图片">
            <template slot-scope="scope">
              <el-image
                style="width: 80px; height: 80px"
                :src="scope.row.water_img"
                fit="cover"
              ></el-image>
            </template>
          </el-table-column> -->
        <el-table-column prop="price" align="center" label="商品单价">
        </el-table-column>
        <el-table-column
          prop="sellingCount"
          align="center"
          label="商品售卖数量"
        >
          <!-- <template slot-scope="scope">
            <span>{{
              scope.row.bill_aftersales_items != null
                ? scope.row.bill_aftersales_items.nums
                : 0
            }}</span>
          </template> -->
        </el-table-column>
        <el-table-column prop="sellingMoney" align="center" label="售卖总价">
          <!-- <template slot-scope="scope">
            <span>{{
              scope.row.bill_aftersales_items != null
                ? scope.row.bill_aftersales_items.amount
                : "0:00"
            }}</span>
          </template> -->
        </el-table-column>
        <!-- <el-table-column prop="amount" align="center" label="退货数量">
          <template slot-scope="scope">
            <span>{{
              scope.row.bill_aftersales_itemss != null
                ? scope.row.bill_aftersales_itemss.nums
                : 0
            }}</span>
          </template>
        </el-table-column> -->
        <el-table-column prop="ctime" align="center" label="统计日期">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              size="mini"
              type="primary"
              >查看</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleClose"
    >
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="packGit"
        border
        style="width: 100%"
      >
        <el-table-column
          type="index"
          align="center"
          :index="indexMethod"
          label="序号"
          width="70"
        >
        </el-table-column>
        <el-table-column prop="order_id" align="center" label="订单编号">
        </el-table-column>
        <el-table-column prop="name" align="center" label="商品名称">
        </el-table-column>
        <el-table-column prop="ctime" align="center" label="下单时间">
        </el-table-column>
        <!-- <el-table-column prop="water_img" align="center" label="水票图片">
          <template slot-scope="scope">
            <el-image
              style="width: 80px; height: 80px"
              :src="scope.row.water_img"
              fit="cover"
            ></el-image>
          </template>
        </el-table-column> -->
        <el-table-column prop="ship_name" align="center" label="收货人名称">
        </el-table-column>
        <el-table-column prop="ship_mobile" align="center" label="收货人手机号">
        </el-table-column>
        <el-table-column prop="price" align="center" label="商品单价">
        </el-table-column>
        <el-table-column prop="nums" align="center" label="商品数量">
        </el-table-column>
        <el-table-column prop="amount" align="center" label="金额">
        </el-table-column>
        <el-table-column prop="numsAfter" align="center" label="售后数量">
        </el-table-column>
        <el-table-column prop="allpriceAfter" align="center" label="售后金额">
        </el-table-column>
      </el-table>
      <div class="pageUser">
        <el-pagination
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"
          :current-page="page1"
          :page-sizes="[10, 150, 200, 300]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total1"
        >
        </el-pagination>
      </div>
    </el-dialog>
  </el-card>
</template>
  
  <script>
// import {
//   packageCount,
//   packageIndex,
//   classification,
// } from "../../../../utils/api.js";
import { goodSort } from "../../../utils/api";
import { shouChuList, shouChuDetail } from "../../../utils/main";
export default {
  data() {
    return {
      options: [],
      Data: [
        {
          name: "水票",
          id: "1",
        },
      ],
      money: "",
      token: sessionStorage.getItem("token"),
      //请求特效
      loading: false,
      //弹窗
      dialogVisible: false,
      form: {
        name: "",
        cat_id: "",
        time: "",
      },
      time: "",
      //发送请求时间参数
      //展示数据
      tableData: [],
      //明细
      packGit: [],
      page: 1,
      limit: 10,
      total: 0,
      page1: 1,
      limit1: 10,
      total1: 0,
      name: "",
      type: 3,
      cat_id: "",
      goods_id: "",
    };
  },
  methods: {
    //日期
    timeChange(e) {
      console.log(e);
      let list = [];
      e.forEach((item) => {
        let date = new Date(item);
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let h = date.getHours();
        let min = date.getMinutes();
        let s = date.getSeconds();
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        h = h < 10 ? "0" + h : h;
        min = min < 10 ? "0" + min : min;
        s = s < 10 ? "0" + s : s;
        list.push(
          year + "-" + month + "-" + day + " " + h + ":" + min + ":" + s
        );
      });

      this.time = list[0] + "," + list[1];
      console.log(this.time);
    },
    //查询
    onSubmit(row) {
      this.name = this.form.name;
      this.time = this.form.time;
      this.cat_id = this.form.cat_id;
      this.page = 1;
      this.getList();
    },
    // 重置
    onReset() {
      this.name = "";
      this.form.name = "";
      this.time = "";
      this.form.time = "";
      this.cat_id = "";
      this.form.cat_id = "";
      this.getList();
    },
    //水票分类
    selectChange() {},
    //分页器
    handleSizeChange(val) {
      this.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },
    handleSizeChange1(val) {
      this.limit1 = val;
      this.packageCounts();
    },
    handleCurrentChange1(val) {
      this.page1 = val;
      this.packageCounts();
    },
    // 序号排序
    Method(index) {
      if (this.total < this.limit) {
        return this.total - index;
      } else {
        return this.total - (this.page - 1) * this.limit - index;
      }
    },
    // 弹出层序号排列
    indexMethod(index) {
      if (this.total1 < this.limit1) {
        return this.total1 - index;
      } else {
        return this.total1 - (this.page1 - 1) * this.limit1 - index;
      }
    },
    // },
    // 获取水票出售列表
    async getList() {
      this.loading = true;
      let res = await shouChuList(
        this.token,
        this.page,
        this.limit,
        this.name,
        this.type,
        this.cat_id,
        this.time
      );
      if (res.data.status) {
        this.tableData = res.data.data.data || [];
        this.total = res.data.data.count;
        this.money = res.data.data.sum;
        this.tableData.forEach((item) => {
          if (
            item.bill_aftersales_items != null &&
            item.bill_aftersales_itemss == null
          ) {
            item.sellingCount = item.bill_aftersales_items.nums;
            item.sellingMoney = item.bill_aftersales_items.amount;
          } else if (
            item.bill_aftersales_items != null &&
            item.bill_aftersales_itemss != null
          ) {
            item.sellingCount =
              item.bill_aftersales_items.nums -
              item.bill_aftersales_itemss.nums;
            item.sellingMoney =
              item.bill_aftersales_items.amount -
              item.bill_aftersales_itemss.amount;
          } else if (
            item.bill_aftersales_items == null &&
            item.bill_aftersales_itemss == null
          ) {
            item.sellingCount = "0";
            item.sellingMoney = "0.00";
          }
        });
        // console.log(this.total, shouchu);
        let timer = setTimeout(() => {
          this.loading = false;
          clearTimeout(timer);
        }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    //获取列表数据
    // packageCounts() {
    //   this.loading = true;
    //   packageCount(
    //     this.token,
    //     this.page,
    //     this.limit,
    //     this.form.mobile,
    //     this.time,
    //     this.form.name
    //   )
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.tableData = res.data.data.data;
    //         this.total = res.data.data.count;
    //         this.tableData.forEach((item) => {
    //           if (item.water_img) {
    //             item.water_img1 = item.water_img;
    //             item.water_img = "https://bjsf.bjsky.top" + item.water_img;
    //           }
    //         });
    //         let timer = setTimeout(() => {
    //           this.loading = false;
    //           clearTimeout(timer);
    //         }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    //明细列表数据
    // packageIndexs() {
    //   this.loading = true;
    //   packageIndex(this.token, this.page1, this.limit1, this.tableData.id, "")
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.packGit = res.data.data.date;
    //         this.total1 = res.data.data.count;
    //         console.log(this.total1);
    //         this.packGit.forEach((item) => {
    //           if (item.water_img) {
    //             item.water_img1 = item.water_img;
    //             item.water_img = "https://bjsf.bjsky.top" + item.water_img;
    //           }
    //         });
    //         let timer = setTimeout(() => {
    //           this.loading = false;
    //           clearTimeout(timer);
    //         }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    //水票分类
    // sdhadj() {
    //   this.loading = true;
    //   classification(this.token)
    //     .then((res) => {
    //       if (res.data.status) {
    //         this.Data = res.data.data.data;

    //         let timer = setTimeout(() => {
    //           this.loading = false;
    //           clearTimeout(timer);
    //         }, 400);
    //       } else {
    //         if (res.data.msg == "token失效,请重新进行登录") {
    //           this.$router.push("/login");
    //         }
    //         if (res.data.msg == "此账号没有权限") {
    //           this.$message.error(res.data.msg);
    //           this.loading = false;
    //         }
    //         let timer = setTimeout(() => {
    //           this.$message.error(res.data.msg);
    //         }, 5000);
    //       }
    //     })
    //     .catch((err) => {
    //       console.log(err.message);
    //     });
    // },
    initialize() {
      goodSort(sessionStorage.getItem("token"), 3, "").then((res) => {
        if (res.data.status) {
          this.options = res.data.data;
          // this.fenleiList.forEach((item, index) => {
          //   console.log(item);
          //   item.child.forEach((item2, index2) => {
          //     if (item2.id == this.fatherObj.goods_cat_id) {
          //       this.selectfenleiList[0] = item.id;
          //       this.selectfenleiList[1] = item2.id;
          //     }
          //   });
          // });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    async packageCounts() {
      let res = await shouChuDetail(
        this.token,
        this.page1,
        this.limit1,
        this.goods_id,
        ""
      );
      console.log(res);
      if (res.data.status) {
        this.packGit = res.data.data.data || [];
        // this.tableData = res.data.data.data || [];
        this.packGit.forEach((item) => {
          if (item.bill_aftersales_itemss != null) {
            item.numsAfter = item.bill_aftersales_itemss.nums;
            item.allpriceAfter = item.bill_aftersales_itemss.amount;
          } else {
            item.numsAfter = "0";
            item.allpriceAfter = "0.00";
          }
        });
        this.total1 = res.data.data.count;
        // // console.log(this.total, shouchu);
        // let timer = setTimeout(() => {
        //   this.loading = false;
        //   clearTimeout(timer);
        // }, 400);
      } else {
        if (res.data.msg == "token失效,请重新进行登录") {
          this.$router.push("/login");
        }
        if (res.data.msg == "此账号没有权限") {
          this.$message.error(res.data.msg);
          this.loading = false;
        }
        let timer = setTimeout(() => {
          this.$message.error(res.data.msg);
        }, 5000);
      }
    },
    // 点击详情的事件
    handleClick(row) {
      this.dialogVisible = true;
      this.goods_id = row.good_id;
      this.packageCounts();
    },
    //关闭弹窗
    handleClose(done) {
      done();
      this.page1 = 1;
      // this.getList();
    },
  },
  mounted() {
    // this.packageCounts();
    // this.sdhadj();
    this.getList();
    this.initialize();
  },
};
</script>
  
  <style scoped>
.salesAmount {
  margin-bottom: 10px;
  margin-right: 10px;
  font-size: 18px;
  color: rgba(249, 100, 34, 1);
}
</style>